/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package objectExtraction;

import com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;

/**
 *
 * @author Shashindra
 */
public class ColourDetector {

    static CvScalar rgba_min = cvScalar(0, 0, 130, 0);
    static CvScalar rgba_max = cvScalar(80, 80, 255, 0);

    public IplImage getThresholdImage(IplImage orgImg) {

        IplImage bacImage = cvCreateImage(cvGetSize(orgImg), 8, 3);
        IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);

        cvInRangeS(orgImg, rgba_min, rgba_max, imgThreshold);// red
        cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 5);

        IplConvKernel rect_12 = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_ELLIPSE, null);
        IplConvKernel rect_6 = cvCreateStructuringElementEx(5, 5, 3, 3, CV_SHAPE_ELLIPSE, null);

        cvMorphologyEx(imgThreshold, imgThreshold, bacImage, rect_6, CV_MOP_CLOSE, 1);
        cvMorphologyEx(imgThreshold, imgThreshold, bacImage, rect_12, CV_MOP_OPEN, 1);

        cvReleaseImage(bacImage);
        cvReleaseStructuringElement(rect_12);
        cvReleaseStructuringElement(rect_6);

        return imgThreshold;
    }
}
